package com.yauchu.kegra;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.content.ClipData;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yauchu.kegra.graph.GraphContainer;
import com.yauchu.kegra.graph.GraphNode;
import com.yauchu.kegra.oelib.Operator;
import com.yauchu.net.LatexImageLoader;
import com.yauchu.toolbar.OperatorsToolbar;

public class KeGra extends Activity {
	/** Called when the activity is first created. */
	// TODO Image Cache, Operator Panel, Operator Storage, Operator Editor, Internodes, Bottom Panel Image, Lazy Refresh, Jabber Export, Collections Editor, 
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LinearLayout main = (LinearLayout) findViewById(R.id.main_view);
        LinearLayout keyboard = (LinearLayout) findViewById(R.id.keyboard);
        
        
        //final ImageView imageViewA= (ImageView) findViewById(R.id.imageViewA);
        //final ImageView imageViewB= (ImageView) findViewById(R.id.imageViewB);
        //final ImageView imageViewP= (ImageView) findViewById(R.id.imageViewP);
        
        
        ImageView urlLatex= (ImageView) findViewById(R.id.UrlLatex);
        
        
        LatexImageLoader lloader=new LatexImageLoader(urlLatex);
		
        //try {
			try {
				lloader.execute("http://www.codecogs.com/png.latex?\\alpha");
				URLEncoder.encode("d","UTF-8");
				} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		//} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		
   
        
        final TextView Akey=(TextView) findViewById(R.id.Akey);
        final TextView Bkey=(TextView) findViewById(R.id.Bkey);
        final TextView Pkey=(TextView) findViewById(R.id.PKey);
        
        
        //ImageView rootImageView= (ImageView) findViewById(R.id.rootFormulaIV);
        //ImageView leftImageView= (ImageView) findViewById(R.id.LeftFormulaIV);
        //ImageView rightImageView= (ImageView) findViewById(R.id.RightFormulaIV);
      //  ImageView rootImageView= new ImageView(this);
      // ImageView leftImageView= new ImageView(this);
      //  ImageView rightImageView= new ImageView(this);
      //  ImageView newImageView= new ImageView(this);
        
        
      //  newImageView.setImageResource(R.drawable.icon);
      //  rootImageView.setImageResource(R.drawable.plus);
      //  leftImageView.setImageResource(R.drawable.a);
      //  rightImageView.setImageResource(R.drawable.b); 
        
      //  rootImageView.setTag("root");
      //  leftImageView.setTag("left");
      //  rightImageView.setTag("right");
             
        
      //  imageViewA.setTag(new Integer(1));
      //  imageViewB.setTag(new Integer(2));
      //  imageViewP.setTag(new Integer(3));
        
        /*
        Bitmap a = BitmapFactory.decodeResource(getResources(), R.drawable.a);
       
        
        Bitmap b =BitmapFactory.decodeResource(getResources(), R.drawable.b);
        Bitmap p =BitmapFactory.decodeResource(getResources(), R.drawable.plus);
        Bitmap i =BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        */
        
        Operator p_operator= new Operator("!arg1+!arg2");
        Operator m_operator= new Operator("!arg1^!arg2");
        Operator mul_operator= new Operator("!arg1*!arg2*!arg3");
        Operator div_operator= new Operator("!arg1/!arg2");
        Operator a_operator= new Operator("a");
        Operator b_operator= new Operator("b");
        Operator c_operator= new Operator("c");
        
       
        
        ArrayList<GraphNode> operatorArray= new ArrayList<GraphNode>();
        
           
        
        operatorArray.add(new GraphNode( new LinearLayout(this.getBaseContext()), p_operator));
        operatorArray.add(new GraphNode( new LinearLayout(this.getBaseContext()), m_operator));
        operatorArray.add(new GraphNode( new LinearLayout(this.getBaseContext()), mul_operator));
        operatorArray.add(new GraphNode( new LinearLayout(this.getBaseContext()), div_operator));
        operatorArray.add(new GraphNode( new LinearLayout(this.getBaseContext()), a_operator));
        
        
        Iterator<GraphNode> opiter= operatorArray.iterator();
        while (opiter.hasNext())
        {
        	opiter.next().switchToColorCodedRepresentation();
        	
        }
        
        
        
        
        
        
        //operatorArray.add(m_operator);
        //operatorArray.add(mul_operator);
        //operatorArray.add(div_operator);
        
 
        LinearLayout central=(LinearLayout) findViewById(R.id.centralLayout);
        LinearLayout central1=(LinearLayout) new LinearLayout(this.getBaseContext());
        LinearLayout central2=(LinearLayout) new LinearLayout(this.getBaseContext());
        LinearLayout central3=(LinearLayout) new LinearLayout(this.getBaseContext());
        LinearLayout central4=(LinearLayout) new LinearLayout(this.getBaseContext());
        LinearLayout central5=(LinearLayout) new LinearLayout(this.getBaseContext());
        
        
        OperatorsToolbar toolbar= new OperatorsToolbar(operatorArray);
        
        keyboard.addView(toolbar.generateView(getBaseContext()));
        
        
        //central1.addView(rootImageView);
        //TextView c1= new TextView(this.getBaseContext());
        //c1.setText("root");
        //central1.addView(c1);
        
        
        
        
        //central2.addView(leftImageView);
        //TextView c2= new TextView(this.getBaseContext());
        //c2.setText("left");
        //central2.addView(c2);
        
        //central3.addView(rightImageView);
        //TextView c3= new TextView(this.getBaseContext());
        //c3.setText("right");
        //central3.addView(c3);
        
        
        //central4.addView(rightImageView);
        //TextView c4= new TextView(this.getBaseContext());
        //c4.setText("ll");
        
        //central4.addView(c4);
        
        
        //central5.addView(rightImageView);
        //TextView c5= new TextView(this.getBaseContext());
        //c5.setText("lr");
        //central5.addView(c5);
        
        central1.setGravity(Gravity.CENTER);
        central2.setGravity(Gravity.CENTER);
        central3.setGravity(Gravity.CENTER);
        central4.setGravity(Gravity.CENTER);
        central5.setGravity(Gravity.CENTER);
        
       /* 
        c1.setGravity(Gravity.CENTER);
        c2.setGravity(Gravity.CENTER);
        c3.setGravity(Gravity.CENTER);
        c4.setGravity(Gravity.CENTER);
        c5.setGravity(Gravity.CENTER);
        
       */
        
        GraphNode rootNode =new GraphNode(central1,p_operator);
        GraphNode leftNode =new GraphNode(central2,m_operator);
        GraphNode rightNode =new GraphNode(central3,a_operator);
        GraphNode leftleftNode =new GraphNode(central4,b_operator);
        GraphNode leftrightNode =new GraphNode(central5,c_operator);

        //GraphNode leftNode =new GraphNode(leftImageView,a,"a");
        //GraphNode rightNode =new GraphNode(central3,"a");
        
        rootNode.displayLayout=central1;
        leftNode.displayLayout=central2;
        rightNode.displayLayout=central3;
        leftleftNode.displayLayout=central4;
        leftrightNode.displayLayout=central5;
        
        GraphContainer gr = new GraphContainer(rootNode);
        
        gr.AddNode(rootNode, rightNode, 2);
        gr.AddNode(rootNode, leftNode, 1);
        gr.AddNode(leftNode,leftleftNode, 1);
        gr.AddNode(leftNode,leftrightNode, 2);
        
        
        TextView stringRep= (TextView) findViewById(R.id.TextRepresentation);
        CharSequence chars=gr.getStringRepresentation();
        
        
        stringRep.setText(chars);
        
        //rootImageView.setOnDragListener(new myDragEventListener(1,gr));
        //leftImageView.setOnDragListener(new myDragEventListener(2,gr));
        //rightImageView.setOnDragListener(new myDragEventListener(3,gr)) ;
        //rootImageView.setImageBitmap(i);
        
       
        
        LinearLayout temp=gr.generateView(rootNode, 2);
        temp.invalidate();
        
        central.addView(temp,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        central.invalidate();
        //central.addView(central2);
        
        
        
        
        
        
//        imageViewA.setOnDragListener(new myDragEventListener(main));
//        imageViewB.setOnDragListener(new myDragEventListener(main));
//        imageViewP.setOnDragListener(new myDragEventListener(main));
        
        
        ///////////////
        
/*        imageViewA.setOnLongClickListener(new View.OnLongClickListener() {

            // Defines the one method for the interface, which is called when the View is long-clicked
            public boolean onLongClick(View v) {

            // Create a new ClipData.
            // This is done in two steps to provide clarity. The convenience method
            // ClipData.newPlainText() can create a plain text ClipData in one step.

            // Create a new ClipData.Item from the ImageView object's tag
            //ClipData.Item item = new ClipData.Item(   (CharSequence) v.getTag());

            // Create a new ClipData using the tag as a label, the plain text MIME type, and
            // the already-created item. This will create a new ClipDescription object within the
            // ClipData, and set its MIME type entry to "text/plain"
            ClipData dragData = ClipData.newPlainText("Name", "A");
            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new DragShadowBuilder(imageViewA);

            // Starts the drag

                    v.startDrag(dragData,  // the data to be dragged
                                myShadow,  // the drag shadow builder
                                null,      // no need to use local data
                                0          // flags (not currently used, set to 0)
                    );
        
        
             return false;
            }
        });
        
        imageViewB.setOnLongClickListener(new View.OnLongClickListener() {

            // Defines the one method for the interface, which is called when the View is long-clicked
            public boolean onLongClick(View v) {

            // Create a new ClipData.
            // This is done in two steps to provide clarity. The convenience method
            // ClipData.newPlainText() can create a plain text ClipData in one step.

            // Create a new ClipData.Item from the ImageView object's tag
            //ClipData.Item item = new ClipData.Item(   (CharSequence) v.getTag());

            // Create a new ClipData using the tag as a label, the plain text MIME type, and
            // the already-created item. This will create a new ClipDescription object within the
            // ClipData, and set its MIME type entry to "text/plain"
            ClipData dragData = ClipData.newPlainText("Name", "B");  
            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new DragShadowBuilder(imageViewB);

            // Starts the drag

                    v.startDrag(dragData,  // the data to be dragged
                                myShadow,  // the drag shadow builder
                                null,      // no need to use local data
                                0          // flags (not currently used, set to 0)
                    );
        
        
             return false;
            }
        });
        imageViewP.setOnLongClickListener(new View.OnLongClickListener() {

            // Defines the one method for the interface, which is called when the View is long-clicked
            public boolean onLongClick(View v) {

            // Create a new ClipData.
            // This is done in two steps to provide clarity. The convenience method
            // ClipData.newPlainText() can create a plain text ClipData in one step.

            // Create a new ClipData.Item from the ImageView object's tag
            //ClipData.Item item = new ClipData.Item(   (CharSequence) v.getTag());

            // Create a new ClipData using the tag as a label, the plain text MIME type, and
            // the already-created item. This will create a new ClipDescription object within the
            // ClipData, and set its MIME type entry to "text/plain"
            ClipData dragData = ClipData.newPlainText("Name", "P");
            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new DragShadowBuilder(imageViewP);

            // Starts the drag

                    v.startDrag(dragData,  // the data to be dragged
                                myShadow,  // the drag shadow builder
                                null,      // no need to use local data
                                0          // flags (not currently used, set to 0)
                    );
        
        
             return false;
            }
        });
        
        
        
        
        
        
        
*/
        
        
        //////////////////
        Akey.setOnLongClickListener(new View.OnLongClickListener() {

            // Defines the one method for the interface, which is called when the View is long-clicked
            public boolean onLongClick(View v) {

            // Create a new ClipData.
            // This is done in two steps to provide clarity. The convenience method
            // ClipData.newPlainText() can create a plain text ClipData in one step.

            // Create a new ClipData.Item from the ImageView object's tag
            //ClipData.Item item = new ClipData.Item(   (CharSequence) v.getTag());

            // Create a new ClipData using the tag as a label, the plain text MIME type, and
            // the already-created item. This will create a new ClipDescription object within the
            // ClipData, and set its MIME type entry to "text/plain"
            ClipData dragData = ClipData.newPlainText("Name", "A");
            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new DragShadowBuilder(Akey);

            // Starts the drag

                    v.startDrag(dragData,  // the data to be dragged
                                myShadow,  // the drag shadow builder
                                null,      // no need to use local data
                                0          // flags (not currently used, set to 0)
                    );
        
        
             return false;
            }
        });

        
        
        
        Bkey.setOnLongClickListener(new View.OnLongClickListener() {

            // Defines the one method for the interface, which is called when the View is long-clicked
            public boolean onLongClick(View v) {

            // Create a new ClipData.
            // This is done in two steps to provide clarity. The convenience method
            // ClipData.newPlainText() can create a plain text ClipData in one step.

            // Create a new ClipData.Item from the ImageView object's tag
            //ClipData.Item item = new ClipData.Item(   (CharSequence) v.getTag());

            // Create a new ClipData using the tag as a label, the plain text MIME type, and
            // the already-created item. This will create a new ClipDescription object within the
            // ClipData, and set its MIME type entry to "text/plain"
            ClipData dragData = ClipData.newPlainText("Name", "B");
            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new DragShadowBuilder(Bkey);

            // Starts the drag

                    v.startDrag(dragData,  // the data to be dragged
                                myShadow,  // the drag shadow builder
                                null,      // no need to use local data
                                0          // flags (not currently used, set to 0)
                    );
        
        
             return false;
            }
        });
        Pkey.setOnLongClickListener(new View.OnLongClickListener() {

            // Defines the one method for the interface, which is called when the View is long-clicked
            public boolean onLongClick(View v) {

            // Create a new ClipData.
            // This is done in two steps to provide clarity. The convenience method
            // ClipData.newPlainText() can create a plain text ClipData in one step.

            // Create a new ClipData.Item from the ImageView object's tag
            //ClipData.Item item = new ClipData.Item(   (CharSequence) v.getTag());

            // Create a new ClipData using the tag as a label, the plain text MIME type, and
            // the already-created item. This will create a new ClipDescription object within the
            // ClipData, and set its MIME type entry to "text/plain"
            ClipData dragData = ClipData.newPlainText("Name", "!arg1+!arg2");
            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = new DragShadowBuilder(Pkey);

            // Starts the drag

                    v.startDrag(dragData,  // the data to be dragged
                                myShadow,  // the drag shadow builder
                                null,      // no need to use local data
                                0          // flags (not currently used, set to 0)
                    );
        
        
             return false;
            }
        });

        
        
        
        
        
        
        
        
        
        
        
        
        
    }
}